package com.jfm.jfmc.core.icc.policy;

import java.util.*;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author DYang
 * @since 2024-10-23 22:58
 */
public class WorkerDiscardPolicy implements RejectedExecutionHandler {

    // 创建一个先进先出的队列存储数据
    private static final Queue<Runnable> queue = new LinkedList<>();

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if (!executor.isShutdown()) {
            queue.add(r);
        }
    }

    /**
     * 获取下一个任务，如果队列为空，则返回null
     * @param task
     * @return
     */
    public static Runnable getNextTask(Runnable task) {
        if (task != null) {
            queue.add(task);
        }
        return queue.poll();
    }


    /**
     * 获取队列大小
     * @return
     */
    public static int getQueueSize() {
        return queue.size();
    }
}
